package com.android.clinic.doctors.app.utils;

import android.app.Activity;
import android.content.Context;

import com.android.clinic.doctors.app.entity.ImageViewInfo;
import com.jiongbull.jlog.JLog;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cheng on 2018/6/19.
 */
public class LocalDataUtils {

    public static Map<Integer, List<ImageViewInfo>> objectMap = new HashMap<>();

    /**
     * 本地保存数据
     *
     * @param context
     * @param file
     * @param object
     * @return
     */
    public static void saveDataToLocate(Context context, String file, Object object) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(file, Activity.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(object);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                    fos.close();
                }
                JLog.e("保存成功");
            } catch (Exception e) {
                JLog.e("没有数据");
                e.printStackTrace();
            }
        }
    }

    /**
     * 本地读取数据
     *
     * @param context
     * @param file
     * @return
     */
    public static Object loadDataFromLocate(Context context, String file) {
        Object obj = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = context.openFileInput(file);
            ois = new ObjectInputStream(fis);
            obj = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            obj = null;
        } finally {
            try {
                if (fis != null) {
                    ois.close();
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return obj;
    }


}
